package com.zsummer.zero.thread;

import java.util.LinkedList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ThreadPoolExcuter��ʹ��
 * @author zhouyujiang
 *
 */
public class ThreadDemo12 {
	
	public static void main(String[] args) throws InterruptedException {
		
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		
		LinkedList<Future<?>> futureList = new LinkedList<Future<?>>();
		for (int i = 0; i < 10; i++) {
			Runnable task = new Runnable() {
				public void run() {
					for (int j = 0; j < 10; j++) {
						try {
							TimeUnit.SECONDS.sleep(1);
							System.out.println(Thread.currentThread().getName() + "-----" + j);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			};
			Future<?> future = executor.submit(task);
			futureList.add(future);
		}
		
		for (Future<?> future : futureList) {
			try {
				future.get();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("end");
		executor.shutdown();
	}
}
